package com.time.company.views;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.time.company.R;


/**
 * 用户名自定义布局
 * Author: Created by deng on 2016/8/18.
 * E-mail: tx16bing@163.com
 */
public class UserLayout extends FrameLayout {

    private EditText etAccount;
    private ImageView ivClear;

    private boolean loginPswEye;
    public UserLayout(Context context) {
        super(context);
    }

    public UserLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        View view = LayoutInflater.from(getContext()).inflate(R.layout.item_user, this, false);
        addView(view);
        initView(view);
    }

    private void initView(final View view) {
        etAccount = view.findViewById(R.id.et_login_account);
        ivClear = view.findViewById(R.id.iv_clear);
        ivClear.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                etAccount.setText("");
            }
        });

        etAccount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (etAccount.getText().length() == 0) {
                    ivClear.setVisibility(View.GONE);
                } else {
                    ivClear.setVisibility(VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        etAccount.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    if (!TextUtils.isEmpty(etAccount.getText())) {
                            ivClear.setVisibility(VISIBLE);
                    }
                } else {
                    ivClear.setVisibility(View.GONE);
                }
            }
        });
    }

    public String getUserName() {
        return etAccount.getText().toString().trim();
    }

}
